#include "thread.hpp"

int TickNum = 10000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *BookTick(void *arg)
{
    const char *name = static_cast<const char *>(arg);
    while (1)
    {
        pthread_mutex_lock(&mutex);
        if (TickNum < 0)
        {
            pthread_mutex_unlock(&mutex);
            return nullptr;
        }
        std::cout << name << " get tick and the rest num of ticks:" << TickNum << std::endl;
        --TickNum;
        pthread_mutex_unlock(&mutex);
        // usleep(1);
    }
    return nullptr;
}

using namespace ThreadModule;
int main()
{

    Thread t1(BookTick, (void *)"thread_1");
    Thread t2(BookTick, (void *)"thread_2");
    Thread t3(BookTick, (void *)"thread_3");

    t1.EnableJoined();
    t2.EnableJoined();
    t3.EnableJoined();

    t1.start();
    t2.start();
    t3.start();

    t1.join();
    t2.join();
    t3.join();
    return 0;
}